package service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dao.BaseDao;
import dao.PaymentDao;
import dao.ProductDao;
import dao.SaleDao;
import dao.SaleItemDao;
import vo.Payment;
import vo.Saleorder;
import vo.Saleorderitem;
import vo.Staff;


public class SaleServiceImpl implements SaleService{
	Saleorder sale;
	StaffService staffservice;
	SaleDao saleDao;
	SaleItemDao saleItemDao;
	ProductDao productDao;
	PaymentDao paymentDao;
	List list = new ArrayList();
	
	public Map createOrder(Saleorder saleorder,Staff staff) {
		Map<String ,Object> map = new HashMap<String,Object>();
		String result = "";
		Date date = new Date();
		long dateNo = date.getTime();
		String orderNo = "so"+dateNo;
		Timestamp createdTime = new Timestamp(date.getTime());
		saleorder.setSaleDateTime(createdTime);
		saleorder.setSaleOrderNo(orderNo);
		saleorder.setPaymentStatus("未支付");
		staff = staffservice.getStaffByNo(staff.getStaffNo());
		saleorder.setStaff(staff);
		if(saleDao.save(saleorder)){
			result = "成功创建新订单";
			
		}else{
			result = "创建订单失败";
		}
		map.put("saleNo", orderNo);
		map.put("result", result);
		map.put("saleOrder", saleDao.getOrderByNo(saleorder.getSaleOrderNo()));
		return map;
	}
	/* (non-Javadoc)
	 * @see service.SaleService#addItem()
	 */
	@Override
	public String addItem(Saleorderitem saleitem) {
		saleitem.setSubTotal(saleitem.getSalePrice()*saleitem.getQuantity());
		System.out.println(saleitem.getItemId());
		saleItemDao.save(saleitem);
		return "增加条目成功";
	}
	
	public Saleorder getSaleByNo(String saleNo){
		String hql = "from Saleorder where saleOrderNo='"+saleNo+"'";
		list = saleDao.select(hql);
		Saleorder sale = (Saleorder)list.get(0);
		return sale;
	}
	/* (non-Javadoc)
	 * @see service.SaleService#getAllOrder()
	 */
	@Override
	public List getAllOrder() {
		List<Saleorder> orderlist = new ArrayList<Saleorder>();
		orderlist = saleDao.findAll();
		/*List<Saleorder> list2 = new ArrayList<Saleorder>();
		for(Saleorder order:orderlist){
			Saleorder newOrder = new Saleorder();
			newOrder = saleDao.getItem(order);
			newOrder.setId(order.getId());
			newOrder.setPayments(order.getPayments());
			newOrder.setPaymentStatus(order.getPaymentStatus());
			newOrder.setSaleDateTime(order.getSaleDateTime());
			newOrder.setSaleOrderNo(order.getSaleOrderNo());
			newOrder.setStaff(order.getStaff());
			newOrder.setTotal(order.getTotal());
			list2.add(newOrder);
		}*/
		return orderlist;
	}


	public List getItem(String saleOrderNo) {	
		List list = new ArrayList();
		List<Saleorderitem> item = new ArrayList<Saleorderitem>();
		item = saleItemDao.getItem(saleOrderNo);
		for(Saleorderitem saleitem:item){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("saleItem",saleitem);
			map.put("product",productDao.getByNo(saleitem.getItemId()));
			list.add(map);
		}
		return list;
	}
	
	
	public List addPayment(Payment payment, Staff staff, Saleorder saleOrder) {
		List list = new ArrayList();
		Date date = new Date();
		long dateNo = date.getTime();
		String paymentNo = "pay"+dateNo;
		payment.setPaymentNo(paymentNo);
		String paytr = "tr"+dateNo;
		payment.setPayTransactionNo(paytr);
		Timestamp payTime = new Timestamp(date.getTime());
		payment.setPayDateTime(payTime);
		staff = staffservice.getStaffByNo(staff.getStaffNo());
		payment.setStaff(staff);
		payment.setSaleorder(saleDao.getOrderByNo(saleOrder.getSaleOrderNo()));
		paymentDao.save(payment);
		changStatus(saleOrder.getSaleOrderNo());
		list.add("success");
		return list;
	}
	
	public void changStatus(String orderNo){
		String sql = "update saleorder set paymentStatus='已支付' where saleOrderNo='"+orderNo+"'";
		saleDao.excuteBySql(sql);
	}
	
	public Saleorder getSale() {
		return sale;
	}
	public void setSale(Saleorder sale) {
		this.sale = sale;
	}
	public StaffService getStaffservice() {
		return staffservice;
	}
	public void setStaffservice(StaffService staffservice) {
		this.staffservice = staffservice;
	}
	
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public SaleDao getSaleDao() {
		return saleDao;
	}
	public void setSaleDao(SaleDao saleDao) {
		this.saleDao = saleDao;
	}
	public SaleItemDao getSaleItemDao() {
		return saleItemDao;
	}
	public void setSaleItemDao(SaleItemDao saleItemDao) {
		this.saleItemDao = saleItemDao;
	}
	public ProductDao getProductDao() {
		return productDao;
	}
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}
	public PaymentDao getPaymentDao() {
		return paymentDao;
	}
	public void setPaymentDao(PaymentDao paymentDao) {
		this.paymentDao = paymentDao;
	}
	
	
}
